package android.textIt.frontend.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.textIt.R;
import android.textIt.backend.dao.GroupDao;
import android.textIt.backend.dao.UserDao;
import android.textIt.backend.model.Group;
import android.textIt.backend.model.User;
import android.textIt.frontend.adapter.GroupMemberAdapter;
import android.textIt.util.TextItUtil;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;

public class GroupMemberActivity extends Activity {
  static UserDao userDAO;
  static GroupDao groupDAO;
	int groupId;
  
	@Override
  public void onCreate(Bundle savedInstanceState) 
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.layout_group_member);
      userDAO = new UserDao(this);
      groupDAO = new GroupDao(this);
      attachButtonOnClickListners(); 
      ListView listView = (ListView)findViewById(R.id.listPotentialMembers);
      List<User> contacts = TextItUtil.getMyContacts(this);
      List<User> members = new ArrayList<User>();
	    Bundle extras = getIntent().getExtras();
	    if (extras.containsKey("id")){
		    groupId = extras.getInt("id");
		    if (groupId > 0){
		    	Group group = groupDAO.getGroupById(groupId);
		    	members = groupDAO.getUsersInGroup(group);
		    }
	    }
  		GroupMemberAdapter gmadapter = new GroupMemberAdapter(this, R.layout.li_members, contacts, members);
      listView.setAdapter(gmadapter);

  }

	private void attachButtonOnClickListners() {
    Button btnSave=(Button)findViewById(R.id.btnSave);
    btnSave.setOnClickListener(new Button.OnClickListener() {
          public void onClick(View v) {
          	Intent result = new Intent();
          	ArrayList<Integer> memberIdList = new ArrayList<Integer>();
            ListView listView = (ListView)findViewById(R.id.listPotentialMembers);
            int count = listView.getAdapter().getCount();
            for (int i = 0; i < count; i++) {
            	 CheckBox chkMember = (CheckBox) listView.getChildAt(listView.getFirstVisiblePosition()+i).findViewById(R.id.chkPotentialMember);
                if (chkMember.isChecked()) {
              		User user = (User) listView.getItemAtPosition(i);
              		memberIdList.add((int) user.getId());
                }
            }
            if (memberIdList.size()==0){
            	new AlertDialog.Builder(GroupMemberActivity.this)
                .setTitle("Error")
                .setMessage("You must select at least one member for the group")
                .setNeutralButton("Ok", new DialogInterface.OnClickListener()
                {
                  public void onClick(DialogInterface dlg, int sumthin)
                  {
                   
                  }
                }).show();
            	return;        	
            }
            else{
              result.putIntegerArrayListExtra("members", memberIdList);
		        	setResult(RESULT_OK, result);
		        	finish();
	          	return; 
            }
          }
      });
    
    Button btnCancel=(Button)findViewById(R.id.btnCancel);
    btnCancel.setOnClickListener(new Button.OnClickListener() {
          public void onClick(View v) {
          	Intent result = new Intent();
	        	setResult(RESULT_CANCELED, result);
	        	finish();
          	return; 
          }
      });	
    }
	

}
